/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.genius.demo;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.LevelListDrawable;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;

public class Menu {
	private Activity activity;
	private int gridviewid;
	private int imageViewId;
	public GridViewAdapter mGridViewAdapter;
	public boolean mIsMore = true;
	public GridView mMenuGrid;
	public MenuItemData mMenuItemData1;
	public MenuItemData mMenuItemData2;
	public View mMenuView;
	public PopupWindow mPopupWindow;
	private int menu_image_list_id_firstlevel;
	private int menu_image_list_id_secondlevel;
	private int menu_itemId;
	private int menu_item_name_array_id_firstlevel;
	private int menu_item_name_array_id_secondlevel;
	private int menu_layoutid;
	private AdapterView.OnItemClickListener onItemClickListener;
	private int px;
	private int textViewId;
	private int viewparent_id;

	public Menu(Activity paramActivity, int paramInt1, int paramInt2,
			int paramInt3, int paramInt4, int paramInt5, int paramInt6,
			int paramInt7, int paramInt8, int paramInt9, int paramInt10,
			AdapterView.OnItemClickListener paramOnItemClickListener) {
		this.menu_item_name_array_id_firstlevel = paramInt1;
		this.menu_image_list_id_firstlevel = paramInt2;
		this.menu_item_name_array_id_secondlevel = paramInt3;
		this.menu_image_list_id_secondlevel = paramInt4;
		this.viewparent_id = paramInt10;
		this.menu_layoutid = paramInt5;
		this.gridviewid = paramInt6;
		this.activity = paramActivity;
		this.imageViewId = paramInt8;
		this.textViewId = paramInt9;
		this.menu_itemId = paramInt7;
		this.onItemClickListener = paramOnItemClickListener;
		this.px = (int) (132.5F * paramActivity.getResources()
				.getDisplayMetrics().density);
		initPopMenu();
	}

	public static int px2dip(Context paramContext, float paramFloat) {
		return (int) (0.5F + paramFloat
				/ paramContext.getResources().getDisplayMetrics().density);
	}

	public void hideMenu() {
		if (!(this.mPopupWindow.isShowing()))
			return;
		this.mPopupWindow.dismiss();
	}

	public void initPopMenu() {
		String[] arrayOfString1 = this.activity.getResources().getStringArray(
				this.menu_item_name_array_id_firstlevel);
		this.mMenuItemData1 = new MenuItemData(
				(LevelListDrawable) this.activity.getResources().getDrawable(
						this.menu_image_list_id_firstlevel), arrayOfString1,
				arrayOfString1.length);
		String[] arrayOfString2 = this.activity.getResources().getStringArray(
				this.menu_item_name_array_id_secondlevel);
		this.mMenuItemData2 = new MenuItemData(
				(LevelListDrawable) this.activity.getResources().getDrawable(
						this.menu_image_list_id_secondlevel), arrayOfString2,
				arrayOfString2.length);
		this.mMenuView = ((LayoutInflater) this.activity
				.getSystemService("layout_inflater")).inflate(
				this.menu_layoutid, null);
		this.mMenuGrid = ((GridView) this.mMenuView
				.findViewById(this.gridviewid));
		this.mGridViewAdapter = new GridViewAdapter(this.activity,
				this.mMenuItemData1, this.menu_itemId, this.imageViewId,
				this.textViewId);
		this.mMenuGrid.setAdapter(this.mGridViewAdapter);
		this.mMenuGrid.setOnItemClickListener(this.onItemClickListener);
		this.mMenuGrid.setOnKeyListener(new View.OnKeyListener() {
			public boolean onKey(View paramView, int paramInt,
					KeyEvent paramKeyEvent) {
				switch (paramInt) {
				case KeyEvent.KEYCODE_MENU:
					Menu.this.showMenuWindow();
				default:
				}
				
				//TODO 是否return false？
				return false;
			}
		});
		this.mPopupWindow = new PopupWindow(this.mMenuView, -1, this.px);
		ColorDrawable localColorDrawable = new ColorDrawable(0);
		this.mPopupWindow.setBackgroundDrawable(localColorDrawable);
		this.mPopupWindow.setAnimationStyle(16973826);
	}

	public void refreshData() {
		if (this.mIsMore) {
			this.mGridViewAdapter.refreshData(this.mMenuItemData1);
			return;
		}
		this.mGridViewAdapter.refreshData(this.mMenuItemData2);
	}

	public void showMenuWindow() {
		if (this.mPopupWindow.isShowing()) {
			this.mPopupWindow.dismiss();
			return;
		}
		this.mGridViewAdapter.refreshData(this.mMenuItemData1);
		this.mPopupWindow.setFocusable(true);
		this.mPopupWindow.setTouchable(true);
		this.mPopupWindow.setOutsideTouchable(true);
		this.mPopupWindow.update();
		this.mPopupWindow.showAtLocation(
				this.activity.findViewById(this.viewparent_id), 80, 0, 0);
	}
}